package com.otod.base.exception;

import java.io.Serializable;

/**
 * 日志框架类
 * @author jqiu
 */
public class FrameworkException extends Exception   implements Serializable{
	private static final long serialVersionUID = -3243158073107841046L;
	
	private String msg;
	private String msgCode;
	protected Throwable cause;

	/**
	 * 框架级异常
	 * @param code	错误号
	 */
	public FrameworkException(String code) {
		this.msg = null;
		this.msgCode = null;
		this.cause = null;

		this.msgCode = code;
//		if(!StringUtil.isEmpty(code)){
//			this.msg = LanguageManager.getMessage(code,null);
//			if(StringUtil.isEmpty(this.msg)){this.msg = code;}
//		}
		this.msg = code;
	}

	/**
	 * 框架级异常
	 * @param code			错误号
	 * @param cause			异常		
	 */
	public FrameworkException(String code, Throwable cause) {
		this(code);
		this.cause = cause;
	}

	/**
	 * 框架级异常
	 * @param code		错误号
	 * @param param		参数
	 */
	public FrameworkException(String code, String param) {
		this(code,new String[]{param});
	}

	/**
	 * 框架级异常
	 * @param code			错误号
	 * @param paramArgs		参数数组
	 */
	public FrameworkException(String code,String[] paramArgs) {
		this.msg = null;
		this.msgCode = null;
		this.cause = null;
		
		this.msgCode = code;
//		if(!StringUtil.isEmpty(code)){
//			this.msg = LanguageManager.getMessage(code,paramArgs);
//			if(StringUtil.isEmpty(this.msg)){this.msg = code;}
//		}
		this.msg = code;
	}

	/**
	 * 框架级异常
	 * @param code			错误号
	 * @param param			参数
	 * @param cause		异常	
	 */
	public FrameworkException(String code, String param,Throwable cause) {
		this(code, param);
		this.cause = cause;
	}

	/**
	 * 框架级异常
	 * @param code			错误号
	 * @param paramArgs		参数数组
	 * @param cause		异常
	 */
	public FrameworkException(String code, String[] paramArgs,Throwable cause) {
		this.msg = null;
		this.msgCode = null;
		this.cause = null;

		this.msgCode = code;

//		if(!StringUtil.isEmpty(code)){
//			this.msg = LanguageManager.getMessage(code,paramArgs);
//			if(StringUtil.isEmpty(this.msg)){this.msg = code;}
//		}
		this.msg = code;
		this.cause = cause;
	}


	/**
	 * 获取原始异常
	 */
	public Throwable getCause() {
		return cause;
	}

	/**
	 * 设置原始异常
	 * @param cause
	 */
	public void setCause(Throwable cause) {
		this.cause = cause;
	}

	
	public String toString() {
		StringBuffer buffer = new StringBuffer(50);
		buffer.append(super.getClass().getName());
		buffer.append("code=");
		buffer.append(getMsgCode());
		if (getMsg() != null) {
			buffer.append(", desc=");
			buffer.append(getMsg());
		}
		if (getCause() != null) {
			buffer.append(", exception is:");
			buffer.append(getCause());
		}
		return buffer.toString();
	}

	/**
	 * 获取异常文字消息
	 * @return
	 */
	public String getMessage() {
		return msg;
	}
	
	/**
	 * 获取异常文字消息
	 * @return
	 */
	public String getMsg() {
		return msg;
	}

	/**
	 * 设置异常文字信息
	 * @param msg
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * 取得异常编号
	 * @return
	 */
	public String getMsgCode() {
		return msgCode;
	}

	/**
	 * 设置异常编号
	 * @param msgCode
	 */
	public void setMsgCode(String msgCode) {
		this.msgCode = msgCode;
	}
	
	/**
	 * 取得异常stack trace
	 * @return	StringBuffer
	 */
	public StringBuffer getStack() {
		StringBuffer stack = new StringBuffer();
		StackTraceElement[] trace = this.getStackTrace();
        for (int i=0; i < trace.length; i++)
            stack.append("\tat " + trace[i] + "<br/>");
        return stack;
	}

}
